package com.google.code.msgbooster.batch.dispatcher;

import com.google.code.msgbooster.batch.Meta;
import com.google.code.msgbooster.model.extension.Consumer;

public abstract class Core extends Meta {
	
	private Consumer consumer;
	
	protected abstract void send(Consumer consumer, Message message) throws Exception;
	protected abstract boolean isRepost(Exception exception); 
	
	protected Result createResult() {
		Result result = new Result();
		result.setRepost(false);
		result.setSuccess(true);
		result.setCause(null);
		return result;
	}

	public void setConsumer(Consumer consumer) {
		this.consumer = consumer;
	}
	
	public final Result send(Message message){
		Result result = createResult();
		try {
			send(consumer, message);
			info(getProperty("msgbooster.batch.dispatcher.core.send.success"));
		} catch (Exception exception) {
			result.setCause(exception.getMessage());
			result.setRepost(isRepost(exception));
			result.setSuccess(false);
			warn(getProperty("msgbooster.batch.dispatcher.core.send.fail"), exception);
		}
		return result;
	}

}
